﻿package com.crl.module.chat.channel
{
	import com.crl.module.chat.model.ChatText;
	import com.crl.module.chat.model.MessageVO;
	import com.crl.utils.StringUtil;
	
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	
	
	/**
	 * 抽像通道;
	 * @author crl
	 * 
	 */	
	public class AbstractChannel implements IChannel
	{
		
		/**
		 * 系统通道; 
		 */		
		public static const CHANNEL_SYSTEM:int=1;
		
		/**
		 * 系统通告,不可被屏蔽; 
		 */		
		public static const CHANNEL_SYSTEM_NOTICE:int=64;
		
		/**
		 * 大喇叭通道; 
		 */		
		public static const CHANNEL_BIGTRUMPET:int = 2;
		
		/**
		 * 小喇叭通道; 
		 */	
		public static const CHANNEL_MINITRUMPET:int = 4;
		
		/**
		 * 组队通道; 
		 */		
		public static const CHANNEL_TEAM:int = 8;
		
		/**
		 * 私有通道; 
		 */		
		public static const CHANNEL_PRIVATE:int = 16;
		
		/**
		 * 普通通道; 
		 */		
		public static const CHANNEL_GENERAL:int = 32;
		
		/**
		 * 表情通道，特殊通道 
		 */		
		public static const CHANNEL_EXPRESSION:int=64;
		
		
		
		public static const LINK_EVENT:String="linkEvent";
		public static var CHAT_PREFIX:Array;
		
		protected var type:int=0;
		protected var name:String;
		
		public var channelColor:Number;
		public var fromColor:Number;
		public var toColor:Number;
		public var messageColor:Number;
		
		/**
		 * 最后发表时间; 
		 */		
		protected var lastSendTime:int;
		protected var timeLimit:int;
		
		
		protected var tips:Dictionary;
		protected var templete:String="[{0}]";
		protected var args:Array;
		
		
		
		/**
		 * 取得配置中指定的信息;
		 * @param index
		 * @return 
		 * 
		 */		
		public static function getChannel_prefixAndColor(index:int):Object{
			if(CHAT_PREFIX==null){
				var info:String="〖私聊〗|FFCC00,〖队内〗|83CA7E,〖小喇叭〗|C9908D,〖当前〗|FF9900,〖系统消息〗|FF0000";
				CHAT_PREFIX=info.split(",");
			}
			
			if(index>=CHAT_PREFIX.length || index<0){
				return {pre:"未知",clr:0xffffff};
			}
			
			var nAc:Array=CHAT_PREFIX[index].split("|");
			
			var color:String=nAc[1];
			if (color && color.indexOf("0x")==-1)
            {
                color = "0x" + color;
            }else{
            	color = "0xffffff";
            }
			return {pre:nAc[0],clr:Number(color)};
		}

		public static function getFromHTML(chatText:ChatText):String{
			var html:String=StringUtil.createLinkTag(LINK_EVENT,chatText.fromName,chatText.fromName,chatText.fromGUID);
			html=StringUtil.createColorHtml("<u>"+html+"</u>",chatText.fromColor.toString(16));
			return html;
		}
		public static function getTOHTML(chatText:ChatText):String{
			var html:String=StringUtil.createLinkTag(LINK_EVENT,chatText.toName,chatText.toName,chatText.toGUID);
			html=StringUtil.createColorHtml("<u>"+html+"</u>",chatText.fromColor.toString(16));
			return html;
		}
		
		
		//public static function escChars
		
		
		public function AbstractChannel(name:String,color:int,timeLimit:int=1000)
		{
			this.name=name;
			this.channelColor=color;
			
			this.fromColor=0x66CCFF;
			this.toColor=0x66CCFF;
			
			this.timeLimit=timeLimit;
			
			
			tips=new Dictionary(true);
			
			tips["timeLimit"]="抱歉，您说得太快了，慢点来！";
			
			args=[];
			
			
		}
		

		public function getType():int{
			return type;
		}
		
		
		/**
		 * 外部调用渲染方法; 
		 * @param chatText
		 * @return 
		 * 
		 */		
		public function parseToString(vo:MessageVO):String
		{
			var chatText:ChatText=getChatText(vo);
			return getPrefix()+parseMessage(chatText);
		}
	
		
		/**
		 * 子类实现功能; 
		 * @param chatText
		 * @return 
		 * 
		 */		
		protected function parseMessage(chatText:ChatText):String{
			var message:String=chatText.message;
			if(chatText.fromGUID){
				message=message.replace(/\{fn\}/g,AbstractChannel.getFromHTML(chatText));
			}
			if(chatText.toGUID){
				message=message.replace(/\{tn\}/g,AbstractChannel.getTOHTML(chatText));
			}
			
			
			return message;
		}
		
		public function getPrefix():String{

			var html:String=StringUtil.createColorHtml(name,channelColor.toString(16));
			return html;
		}
		
		
		
		
		/**
		 * 取得发送信息; 
		 * @param messagevo
		 * @return 
		 * 
		 */		
		private function getChatText(messagevo:MessageVO):ChatText
		{
			var chatText:ChatText=new ChatText(this.type,this.channelColor,
			messagevo.fk,messagevo.fn,fromColor,
			messagevo.tk,messagevo.tn,toColor,
			messagevo.m,messageColor);
			
			return chatText;
		}
		
		
		public function check():void{
			var time:int = getTimer();
			if(time - lastSendTime < timeLimit){
				throw new Error(getTipByKey("timeLimit"));
			}
			lastSendTime = time;
		}
		
		public function getTipByKey(key:String):String{
			return tips[key];
		}
		
		public function isClosed():Boolean
		{
			return false;
			
		}
		
		public function getName():String
		{
			return this.name;
		}
		
	}
}